const db = require("@/db/mysql");
const PublicModel = require("../../common/public.model");

class AuthModel extends PublicModel {
	// 账户登录
	async login({ userName, password }) {
		return db.query(`SELECT * FROM system_users WHERE username='${userName}' AND password='${password}'`);
	}

    /**
     * 通过用户
     * @param {*} userid 
     * @returns 
     */
    getUser(userid) {
        const SQL = `
            SELECT
                id,
                name,
                phone,
                status,
                type,
                is_super_admin,
                last_login_time,
                lock_start_time,
                lock_end_time
            FROM
                users
            WHERE 
                ${userid ? `id = '${userid}' ` : ''}
                AND is_delete = 0
        `;
		return db.query(SQL);
    }

	/**
	 * 查询用户id
	 * @param {*} id
	 * @returns
	 */
	getUserInfoById(id) {
		const SQL = `
            SELECT
                id,
                username,
                name,
                phone 
            FROM
                system_users
            WHERE
                id = '${id}' 
                AND is_delete = 0
        `;
		return db.query(SQL);
	}

	/**
	 * 通过openid获取用户信息
	 */
	getUserInfoByOpenid(openid) {
		const SQL = `
            SELECT
                id,
                name,
                nick_name,
                phone,
                real_name,
                avatar_image,
                is_privacy_agreement,
                is_super_admin
            FROM
                users
            WHERE
                wx_openid = '${openid}' 
                AND is_delete = 0
        `;
		return db.query(SQL);
	}

    /**
     * 创建用户
     * @param {*} param0 
     */
	createWxUser({ id, name, phone, nick_name, real_name, wx_openid, avatar_image, type, create_user }) {
		const SQL = `
            INSERT INTO
            users (
                id, name, phone, nick_name, real_name, wx_openid, avatar_image, type, create_time, create_user
            ) VALUE (
                '${id}', '${name}', '${phone}', '${nick_name}', '${real_name}', '${wx_openid}', '${avatar_image}', ${type}, '${this.time}', '${create_user}'
            )
        `;
        return this.query(SQL);
	}

    /**
     * 创建pc用户
     * @param {*} param0 
     * @returns 
     */
    createPcUser({ id, name, password, phone, nick_name, real_name, wx_openid, avatar_image, type, create_user }) {
        const SQL = `
            INSERT INTO
            users (
                id, name, password, phone, nick_name, real_name, wx_openid, avatar_image, type, create_time, create_user
            ) VALUE (
                '${id}', '${name}', '${password}', '${phone}', '${nick_name}', '${real_name}', '${wx_openid}', '${avatar_image}', ${type}, '${this.time}', '${create_user}'
            )
        `;
        return this.query(SQL);
    }

    // 检测用户名是否存在
    checkUserNameExist(name) {
        const SQL = `SELECT username FROM system_users WHERE username = '${username}' AND is_delete = 0`;
        return this.query(SQL)
    }

    /**
     * 设置用户隐私协议
     * @param {*} uid 
     * @returns 
     */
    setUserPrivacyAgreement(uid) {
        const SQL = this.sql(`UPDATE users SET is_privacy_agreement = 1 WHERE id = '${uid}' AND is_delete = 0`);
        return this.query(SQL);
    }
}

module.exports = {
	authModel: new AuthModel()
};
